package com.chenfan.process.service.impl;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenfan.process.common.constants.CommonFieldConstants;
import com.chenfan.process.common.utils.QueryWrapperUtils;
import com.chenfan.process.entity.po.ProcessRelatedPo;
import com.chenfan.process.enums.ProcessResultTypeEnums;
import com.chenfan.process.repository.ProcessRelatedRepository;
import com.chenfan.process.service.ProcessRelatedService;

/**
 * @author K
 * @date 2021/3/4
 */
@Service
public class ProcessRelatedServiceImpl extends ServiceImpl<ProcessRelatedRepository, ProcessRelatedPo>
    implements ProcessRelatedService {

    @Override
    public ProcessRelatedPo findHead(Long approvalTemplateId, Long version) {
        QueryWrapper<ProcessRelatedPo> queryWrapper = QueryWrapperUtils.newQueryWrapper();
        queryWrapper.eq(ProcessRelatedPo.APPROVAL_TEMPLATE_ID, approvalTemplateId);
        queryWrapper.orderByAsc(CommonFieldConstants.CREATE_TIME);
        queryWrapper.eq(ProcessRelatedPo.VERSION, version);
        List<ProcessRelatedPo> list = list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        Optional<ProcessRelatedPo> any = list.stream().filter(e -> e.getProcessStatus().equals(ProcessResultTypeEnums.PARENT.getIndex())).findAny();
        return any.orElse(list.get(0));
    }

    @Override
    public List<ProcessRelatedPo> listProcessRelatedAfterProcessId(Long approvalTemplateId, Long version, Long processId) {
        return listProcessRelatedAfterProcessId(approvalTemplateId,version,processId,null);
    }
    @Override
    public List<ProcessRelatedPo> listProcessRelatedAfterProcessId(Long approvalTemplateId, Long version, Long processId,Long groupId) {
        QueryWrapper<ProcessRelatedPo> queryWrapper = QueryWrapperUtils.newQueryWrapper();
        queryWrapper.eq(ProcessRelatedPo.APPROVAL_TEMPLATE_ID, approvalTemplateId);
        if (Objects.nonNull(processId)) {
            queryWrapper.eq(ProcessRelatedPo.PROCESS_BEFORE_ID, processId);
        }
        if (Objects.nonNull(groupId)) {
            queryWrapper.eq(ProcessRelatedPo.GROUP_ID, groupId);
        }
        queryWrapper.eq(ProcessRelatedPo.VERSION, version);
        return list(queryWrapper);
    }

    @Override
    public List<ProcessRelatedPo> listWithGroup(Long approvalTemplateId, Long version, Long groupId) {
        QueryWrapper<ProcessRelatedPo> queryWrapper = QueryWrapperUtils.newQueryWrapper();
        queryWrapper.eq(ProcessRelatedPo.APPROVAL_TEMPLATE_ID, approvalTemplateId);
        queryWrapper.eq(CommonFieldConstants.VERSION, version);
        queryWrapper.eq(ProcessRelatedPo.GROUP_ID, groupId);
        return list(queryWrapper);
    }

    @Override
    public List<ProcessRelatedPo> listProcessRelatedWithTemplate(List<Long> approvalTemplateIds) {
        QueryWrapper<ProcessRelatedPo> queryWrapper = QueryWrapperUtils.newQueryWrapper();
        queryWrapper.in(ProcessRelatedPo.APPROVAL_TEMPLATE_ID, approvalTemplateIds);
        return list(queryWrapper);
    }
}
